package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.object.pojo.AgentDepositVip;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.service.business.AgentDepositVipService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.LogUtil;
import com.tbit.utils.DateTimeUtil;
import com.tbit.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author: chen
 * @Description: 用户区域免押
 * @Date: 创建于 10:24 2020/6/17
 */
@RestController
@RequestMapping("/agentDepositVip")
public class AgentDepositVipController extends BaseController {
    @Autowired
    private AgentDepositVipService agentDepositVipService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 分页
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(Integer accountId, String phone, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {

        List<AgentDepositVip> list = agentDepositVipService.queryPage(accountId, phone, pageNO, rowCount);
        Integer total = agentDepositVipService.queryCount(accountId, phone);
        return JsonResult.ok(list, total);
    }

    /**
     * 新增
     */
    @RequestMapping("/insert")
    @OperateLog(module = ModuleEnum.FREETIME, methods = MethodEnum.FREETIME_ADD)
    public JsonResult insert(AgentDepositVip agentDepositVip, Long expireTime, String operaPsw, HttpServletRequest request) {
        Assert.notNull(agentDepositVip.getUserId(), "ver.userId.notNull");
        Assert.notNull(agentDepositVip.getAccountId(), "ver.adAccountId.notNull");
        Assert.notNull(expireTime, "ver.expireTime.notNull");
        String date = DateTimeUtil.format(new Date(expireTime), DateUtil.YMD_);
        agentDepositVip.setExpireDt(date);
        LoginInfo loginInfo = getLogin(request);
        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_COUPONS_CONFIG.getModule(), operaPsw);

        agentDepositVipService.insert(agentDepositVip);

        LogUtil.info(loginInfo.id + "新增用户免押卡,用户id=" + agentDepositVip.getUserId());
        return JsonResult.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @OperateLog(module = ModuleEnum.FREETIME, methods = MethodEnum.FREETIME_EDIT)
    public JsonResult edit(AgentDepositVip agentDepositVip, Long expireTime, String operaPsw,
                           HttpServletRequest request) {
        Assert.notNull(agentDepositVip.getUserId(), "ver.userId.notNull");
        Assert.notNull(expireTime, "ver.expireTime.notNull");
        Assert.notNull(agentDepositVip.getAccountId(), "ver.adAccountId.notNull");
        String date = DateTimeUtil.format(new Date(expireTime), DateUtil.YMD_);
        agentDepositVip.setExpireDt(date);
        LoginInfo loginInfo = getLogin(request);
        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_COUPONS_CONFIG.getModule(), operaPsw);

        agentDepositVipService.edit(agentDepositVip);

        LogUtil.info(loginInfo.accountUserId + "修改用户免押卡,用户id=" + agentDepositVip.getUserId());
        return JsonResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.FREETIME, methods = MethodEnum.FREETIME_DEL)
    public JsonResult del(Integer userId, Integer accountId, String operaPsw, HttpServletRequest request) {
        Assert.notNull(userId, "ver.userId.notNull");
        Assert.notNull(accountId, "ver.adAccountId.notNull");
        agentDepositVipService.del(userId, accountId);

        LoginInfo loginInfo = getLogin(request);
        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_COUPONS_CONFIG.getModule(), operaPsw);

        LogUtil.info(loginInfo.id + "删除用户免押卡,用户id=" + userId);
        return JsonResult.ok();
    }


}
